package ai.mspbots.test.service;

import ai.mspbots.test.dto.AzureAIConfig;
import ai.mspbots.test.entity.TenantIntegration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Azure AI配置管理服务
 * 
 * @author mspbots
 * @since 1.0.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AzureConfigManagementService {

    private final AzureAIVerificationService azureVerificationService;
    private final AzureAIConfigService azureAIConfigService;
    private final TenantIntegrationService tenantIntegrationService;

    /**
     * 验证Azure AI配置并保存
     */
    public boolean validateAndSaveAzureConfig(AzureAIConfig config) {
        boolean isValid = azureVerificationService.verifyAzureAIKey(config);

        if (isValid) {
            azureAIConfigService.save(config);
            log.info("Azure配置验证成功并保存: {}", config.getId());
            return true;
        } else {
            log.warn("Azure配置验证失败: {}", config);
            return false;
        }
    }

    /**
     * 批量验证Azure配置
     */
    public void batchValidateAzureConfigs(Long integrationId) {
        List<AzureAIConfig> configs = tenantIntegrationService.getByIntegrations(integrationId);

        for (AzureAIConfig config : configs) {
            boolean isValid = azureVerificationService.verifyAzureAIKey(config);
            if (!isValid) {
                log.warn("Azure配置验证失败: {}", config);
            } else {
                azureAIConfigService.save(config);
                log.info("Azure配置验证成功并保存: {}", config);
            }
        }
    }

    /**
     * 获取Azure AI配置列表
     */
    public List<AzureAIConfig> getAzureAIConfigs() {
        List<AzureAIConfig> azureAIConfigs = azureAIConfigService.list();
        log.info("获取到 {} 个Azure AI配置", azureAIConfigs.size());
        return azureAIConfigs;
    }
}
